//
//  ToolImageEditViewController.m
//  BDD_TWO
//
//  Created by XTW on 2023/6/9.
//

#import "ToolImageEditViewController.h"
#import "UIImage+LF_Format.h"
#import "LFMEGIFImageSerialization.h"
#import "LFPhotoEditingController.h"
#import "UIImage+LFMECommon.h"

@interface ToolImageEditViewController ()<LFPhotoEditingControllerDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *editBtn;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UIView *infoView;
@property (nonatomic, strong) UIImage *image;
/** 需要保存到编辑数据 */
@property (nonatomic, strong) LFPhotoEdit *photoEdit;
@property (nonatomic, strong) NSArray<NSNumber *> *durations;

@end

@implementation ToolImageEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"图形编辑";
    [self addBackBtn];
    [((UIButton *)[self addRigthBtnImage:[UIImage systemImageNamed:@"plus"]]) addTarget:self action:@selector(addPress) forControlEvents:UIControlEventTouchUpInside];
    [self initView];
    // Do any additional setup after loading the view from its nib.
}

- (void)addPress {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

- (IBAction)editPress {
    if (!self.infoView.hidden) {
        [self showHUDAlert:@"未添加图片"];
        return;
    }
    LFPhotoEditingController *lfPhotoEditVC = [[LFPhotoEditingController alloc] init];
//    lfPhotoEditVC.operationType = LFPhotoEditOperationType_draw | LFPhotoEditOperationType_splash;
//    lfPhotoEditVC.defaultOperationType = LFPhotoEditOperationType_crop; // 默认剪裁
    // 以下属性建议逐个开放测试。
    lfPhotoEditVC.operationAttrs = @{
//                                     LFPhotoEditDrawColorAttributeName:@(LFPhotoEditOperationSubTypeDrawVioletRedColor), // 绘画紫罗兰红色
//                                     LFPhotoEditDrawBrushAttributeName:@(LFPhotoEditOperationSubTypeDrawStampFruitBrush), // 绘画笔刷
//                                     LFPhotoEditStickerContentsAttributeName:@[
//                                             [LFStickerContent stickerContentWithTitle:@"默认" contents:@[
//                                                 appendFile,
//                                                 failFile,
//                                                 LFStickerContentDefaultSticker]],
//                                             [LFStickerContent stickerContentWithTitle:@"相册" contents:@[
//                                                 LFStickerContentAllAlbum
//                                             ]],
//                                             [LFStickerContent stickerContentWithTitle:@"GIF" contents:stickerGifs]
//                                                                                ], // 贴图资源
//                                     LFPhotoEditTextColorAttributeName:@(LFPhotoEditOperationSubTypeTextAzureColor), // 字体天蓝色
//                                     LFPhotoEditSplashAttributeName:@(LFPhotoEditOperationSubTypeSplashPaintbrush), //涂抹效果
//                                     LFPhotoEditFilterAttributeName:@(LFPhotoEditOperationSubTypeProcessFilter), //滤镜效果
//                                     LFPhotoEditCropAspectRatioAttributeName:@(LFPhotoEditOperationSubTypeCropAspectRatio1x1), //剪裁尺寸
//                                     LFPhotoEditCropCanRotateAttributeName:@(NO), //不允许剪切旋转
//                                     LFPhotoEditCropCanAspectRatioAttributeName:@(NO), //不允许剪切比例调整
//                                     LFPhotoEditCropExtraAspectRatioAttributeName:@[
//                                             [LFExtraAspectRatio extraAspectRatioWithWidth:9 andHeight:16 autoAspectRatio:NO],
//                                             [LFExtraAspectRatio extraAspectRatioWithWidth:9 andHeight:16],
//                                             [LFExtraAspectRatio extraAspectRatioWithWidth:16 andHeight:9],
//                                     ], //自定义纵横比
                                     };
    
    lfPhotoEditVC.delegate = self;
    LFPhotoEdit *photoEdit = self.photoEdit;
    if (photoEdit) {
        lfPhotoEditVC.photoEdit = photoEdit;
    } else {
        [lfPhotoEditVC setEditImage:self.image durations:self.durations];
    }
    [self.navigationController setNavigationBarHidden:YES];
    [self.navigationController pushViewController:lfPhotoEditVC animated:NO];
}

- (IBAction)savePress {
    if (!self.infoView.hidden) {
        [self showHUDAlert:@"未添加图片"];
        return;
    }
    UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);

}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (!error) {
        [self showHUDAlert:@"已保存到系统相册"];
    }else{
        [self showHUDAlert:@"保存失败"];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    //并且赋值给声明好的imageView
    self.imageView.image = self.image;
    self.photoEdit = nil;
    //最后模态返回 最初的 控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
    self.infoView.hidden = YES;
}

#pragma mark - LFPhotoEditingControllerDelegate
- (void)lf_PhotoEditingControllerDidCancel:(LFPhotoEditingController *)photoEditingVC {
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)lf_PhotoEditingController:(LFPhotoEditingController *)photoEditingVC didFinishPhotoEdit:(LFPhotoEdit *)photoEdit {
    [self.navigationController popViewControllerAnimated:NO];
    self.image = photoEdit.editPreviewImage;
    self.imageView.image = self.image;
    self.photoEdit = photoEdit;
}

- (void)initView {
    [self.imageView setFrame:CGRectMake(0, IS_iPhoneX_Top + NavBarHeight, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE - IS_iPhoneX_Top - NavBarHeight - IS_iPhoneX_Bottom - 44 - 20)];
    [self.bottomView setFrame:CGRectMake(0, SCREEN_HEIGHT_DEVICE - IS_iPhoneX_Bottom - 44 - 10, SCREEN_WIDTH_DEVICE, 44)];
    [self.infoView setFrame:CGRectMake(SCREEN_WIDTH_DEVICE - 157, IS_iPhoneX_Top + NavBarHeight, 157, 103)];
}

@end
